"use strict";

const MASTERBRANCH = {
    "_id": "",
    "name": "上帝模式",
    "op": 1,
    "dev": 0
};

 

//进入运营后台 
module.exports = async function() {

    // //debug
    // this.view='proxy/roleDetail';
//    this.view = 'main';
    let level = this.session.get("level");
    let branch = this.get("branch", "string");

    let result = {
        "name": this.session.get("name"),
        "level": this.session.get("level"),
        "branch": {}
    };
    let authorize = this.model.admin("authorize");
    let ret=await authorize.branch(this.session.uid, result['level'])
    result['branch'] = Object.assign(result['branch'], ret || {});
    // if(!result['branch'][branch] ){
    //     return this.error("authorize not exist");
    // }
    // result['title'] = result['branch'][branch]['name'];
    if (branch !== MASTERBRANCH["_id"]) {
        result['bpath'] = ['', 'b', branch].join('/');
        result['select'] = branch;
    } else {
        result['bpath'] = '';
        result['select'] = null;
    }

    //万能表单
    result['forms'] = {};
    let navbar = this.config("navbar");
    for (let k in navbar) {
        result['forms'][k] = [];
    }
    if (!branch) {
        return this.success(result);
    }
    let formsModel = this.model.branch("forms/navbar", branch);
    ret=await formsModel.get(null, ['name', 'mid']);
    if (ret) {
        for (let k in ret) {
            let v = ret[k];
            let m = v['mid'];
            if (result['forms'][m]) {
                result['forms'][m].push(v);
            }
        }
    }
    ret =await authorize.find({"uid":this.session.uid});
    if(ret){
        result=Object.assign(result,ret);
    }
    let setting = this.model.admin('setting');
    result.setting={};
    ret=await setting.find({});
    result.setting=Object.assign(result.setting,ret||{});
    result.title=result.setting.name||this.config.get('dbname');
    return result;
}




//选择应用1.1
module.exports.selectNew = async function() {
    this.view = 'workDesk';
    let uid = this.session.uid,
        level = this.session.get("level"),
        authorize = this.model.admin("authorize");
    let result = {
        name: this.session.get('name'),
        level: level,
        uid: uid
    };


    result['name'] = this.session.get('name');
    result['games'] = {};
    result['barType'] = 'right';

    let _ret=await authorize.find({"uid":uid});
    if(_ret){
          result=Object.assign(result,_ret);
    }
  




    if (level >= 100) {
            this.output = 'redirect';
            let url = '/main/?branch=';
            return url;
        // //扫地僧
        // const branchM = this.model.admin('branch');
        // const keys = ['_id', 'name'];
        // const query = {};
        // const option = {
        //     "multi": true,
        //     "fields": keys,
        //     "key": "_id"
        // };
        // return branchM.find(query, option).then(ret => {
        //     if (ret) {
        //         result['games'] = Object.assign(result['games'], ret || {})
        //         for(let i in result['games']){
        //             result['games'][i].op=1;
        //             result['games'][i].dev=1;
        //         }
        //     }
        //     return result;
        // })
    }
    else {
        return authorize.branchNew(uid, level).then(ret => {
            result['games'] = Object.assign(result['games'], ret || {})
            return result;
        })
    }
}